package com.netty.code.self.resp.groupChar;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * TODO
 * @author wuzz
 * @since 2024/7/11
 */
public class Client {


    private SocketChannel socketChannel;
    private Selector selector;
    private String username;

    public Client(String username) {
        try {
            this.selector = Selector.open();
            this.socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("127.0.0.1", 8888));
            socketChannel.configureBlocking(false);
            socketChannel.register(selector, SelectionKey.OP_READ);
            this.username = username;
            System.out.println(username + "   准备就绪");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        int select = selector.select();
                        if (select > 0) {
                            Set<SelectionKey> selectionKeys = selector.selectedKeys();
                            Iterator<SelectionKey> iterator = selectionKeys.iterator();
                            while (iterator.hasNext()) {
                                SelectionKey key = iterator.next();
                                if (key.isReadable()) {
                                    SocketChannel channel = (SocketChannel) key.channel();
                                    ByteBuffer allocate1 = ByteBuffer.allocate(1024);
                                    int read = channel.read(allocate1);
                                    String s = new String(allocate1.array());
                                    System.out.println(s);
                                }
                            }
                            iterator.remove();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    // 发送消息
    public void sendMsg(String msg) {
        msg = username + "::" + msg;
        try {
            socketChannel.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void rever() {
        try {
            //
            int select = selector.select();
            if (select > 0) {
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    if (key.isReadable()) {

                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        Client client = new Client("b");
        client.start();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            client.sendMsg(s);
        }

    }

}
